package com.android.tvremoteplay;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by kingt on 2018/3/6.
 */

public class Environment {
    public final static boolean needDebug = true;
    public  static DkToast toast;
    public static int adbServerPort = 5555;

    private static Handler toastHandler = null;

    public static void debug(String tag, String msg){
        Log.d(tag, msg);
    }
    public static void debug(String tag, String msg, Throwable tr){
        Log.d(tag, msg, tr);
    }

    public static void initToastHandler(){
        if(toastHandler == null) toastHandler = new Handler();
    }
    public static void toastInHandler(final Context context, final String msg,final String text){
        if(toastHandler != null){
            toastHandler.post(new Runnable() {
                @Override
                public void run() {
                    toast(context, msg,text);
                }
            });
        }
    }
    public static void toastInHandler(final Context context, final String msg){
        if(toastHandler != null){
            toastHandler.post(new Runnable() {
                @Override
                public void run() {
                    toast(context, msg);
                }
            });
        }
    }
    public static void toast(Context context, String msg,String text){
//        LayoutInflater inflater = LayoutInflater.from(context);
//        View view = inflater.inflate(R.layout.toast, null);
//        TextView t = (TextView) view.findViewById(R.id.toast_text);
//        t.setText(text);
//        if (toast != null) {
//            toast.cancel();
//        }
//        toast = new Toast(context);
//        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.setView(view);
//        toast.show();
//        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        ToastView(context,msg,text);
    }
    public static void toast(Context context, String msg){
//        LayoutInflater inflater = LayoutInflater.from(context);
//        View view = inflater.inflate(R.layout.toast, null);
//        TextView t = (TextView) view.findViewById(R.id.toast_text);
//        t.setText(text);
//        if (toast != null) {
//            toast.cancel();
//        }
//        toast = new Toast(context);
//        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.setView(view);
//        toast.show();

       // DkToast.makeDkToast(context,"显示效果",100000).show();
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
//        ToastView(context,msg,text);
    }

   /* public static void ToastView(Context context, String text) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast, null);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }*/

    public static  void  ToastView(Context context, String text,String text2) {
//        LayoutInflater inflater = LayoutInflater.from(context);
//        View view = inflater.inflate(R.layout.toast, null);
//        ImageView imageView=(ImageView)view.findViewById(R.id.toast_image);
//        if (text2.equals("高温预警")){
//            imageView.setBackgroundResource(R.drawable.test);
//        }
//        if (text2.equals("暴雨预警")){
//            imageView.setBackgroundResource(R.drawable.test2);
//        }
//        TextView t = (TextView) view.findViewById(R.id.toast_text);
//        t.setText(text);
//        if (toast != null) {
//            toast.cancel();
//        }
////        DkToast.makeDkToast(context,"显示效果",100000).show();
////        DkToast =
//        toast = new DkToast(context);
////        toast.setDuration(100000);
//        toast.setGravity(Gravity.CENTER,0,0);
//        toast.setDuration(Toast.LENGTH_LONG);
//        toast.setView(view);
//        toast.show();
        DkToast.makeDkToast(context,text,text2).show();
    }

    public static boolean isEnableIME(Context context){
        try {
            InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            List<InputMethodInfo> inputs = imm.getEnabledInputMethodList();
            boolean flag = false;
            for(InputMethodInfo input : inputs){
                if(input.getPackageName().equals(IMEService.class.getPackage().getName())){
                    return true;
                }
            }
        }catch (Exception ignored){ }
        return false;
    }

    public  static boolean isDefaultIME(Context context){
        try {
            String defaultImme = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);

            if (defaultImme != null && defaultImme.startsWith(IMEService.class.getPackage().getName())) {
                return true;
            }
        }catch (Exception ignored){ }
        return false;
    }
}
